let myAjax = {
    post: (url, query, callback, isJson) => {
        let str = ''
        if (query) {
            for (const key in query) {
                str += `${key}=${query[key]}&`
            }
            str = str.slice(0, -1)
        }
        let xhr = new XMLHttpRequest()
        xhr.open('post', url)
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        xhr.send(str)
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                let res = isJson ? JSON.parse(xhr.responseText) : xhr.responseText
                callback(res)
            }
        }
    },
    get: (url, query, callback, isJson) => {
        if (query) {
            url += '?'
            for (key in query) {
                url += `${key}=${query[key]}&`
            }
            url = url.slice(0, -1)
        }
        let xhr = new XMLHttpRequest()
        xhr.open('get', url)
        xhr.send()
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4 && xhr.status === 200) {
                let res = isJson ? JSON.parse(xhr.responseText) : xhr.responseText
                callback(res)
            }
        }
    },
    ajax: (params) => {
        let xhr = new XMLHttpRequest()
        if (params.method.toLowerCase() === 'get') {
            if (params.query) {
                url += '?'
                for (key in params.query) {
                    url += `${key}=${params.query[key]}&`
                }
                url = url.slice(0, -1)
            }
            xhr.open('get', params.url)
            xhr.send()
        } else {
            let str = ''
            if (params.query) {
                for (const key in params.query) {
                    str += `${key}=${params.query[key]}&`
                }
                str = str.slice(0, -1)
            }
            xhr.open('post', params.url)
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
            xhr.send(str)
        }
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4 && xhr.status === 200) {
                let res = params.isJson ? JSON.parse(xhr.responseText) : xhr.responseText
                params.callback(res)
            }
        }
    },
    fetch: (_method, _url, _data) => {
        return new Promise((resolve, reject) => {
            let xhr = new XMLHttpRequest()
            let data = _data || null
            let method = _method || 'get'

            if (data) {
                _url += '?'
                for (key in data) {
                    _url += `${key}=${data[key]}&`
                }
                _url = _url.slice(0, -1)
            }
            // console.log(_method, _url, _data);
            xhr.open(method, _url, true)
            xhr.send()
            xhr.onreadystatechange = () => {
                console.log(xhr.readyState, xhr.status);
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        try {
                            let res = JSON.parse(xhr.responseText)
                            resolve(res)
                        } catch (e) {
                            reject(e)
                        }
                    } else {
                        reject(new Error(xhr.statusText))
                    }
                }
            }
        })
    }
}